//
//  OSTopic.m
//  百思不得姐
//
//  Created by PushMeBaby on 16/3/2.
//  Copyright © 2016年 CoderC. All rights reserved.
//

#import "OSTopic.h"

#import <MJFoundation.h>


@implementation OSTopic



{
    CGFloat _cellHeight;
    CGRect _pictureF;
}

+(NSDictionary*)mj_replacedKeyFromPropertyName{
    return @{@"small_image" : @"image0",
             @"largr_image" : @"image1",
             @"middle_image" : @"image2",
             @"ID" : @"id",
             @"top_cmt" : @"top_cmt[0]" };
}
-(NSString *)create_time{
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *create = [fmt dateFromString:_create_time];
    if ([[UIDevice currentDevice] systemVersion].floatValue < 8.0) {
        return [fmt stringFromDate:create];
    }
    if (create.isThisYear) {
        if (create.isToday) {
            NSDateComponents *cmps = [[NSDate date] deltaFrom:create] ;
            if (cmps.hour >= 1) {
                return [NSString stringWithFormat:@"%zd小时前",cmps.hour];
            }else if (cmps.minute >= 1){
                return [NSString stringWithFormat:@"%zd分钟前",cmps.minute];
            }else{
                return @"刚刚";
            }
        }else if (create.isYesterday){
            fmt.dateFormat = @"昨天 HH:mm:ss";
            return [fmt stringFromDate:create];
        }else{
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            return [fmt stringFromDate:create];
        }
    }else{
        return _create_time;
    }
}

- (CGFloat)cellHeight
{
    if (!_cellHeight) {
        // 文字的最大尺寸
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * OSTopicCellMargin, MAXFLOAT);
        // 计算文字的高度
        CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
        
        // cell的高度
        // 文字部分的高度
        _cellHeight = OSTopicCellTextY + textH + OSTopicCellMargin;
        
        // 根据段子的类型来计算cell的高度
        if (self.type == OSTopicTypePicture) { // 图片帖子
            // 图片显示出来的宽度
            CGFloat pictureW = maxSize.width;
            // 显示显示出来的高度
            CGFloat pictureH = pictureW * self.height / self.width;
            if (pictureH >= OSTopicCellPictureMaxH) { // 图片高度过长
                pictureH = OSTopicCellPictureBreakH;
                self.bigPicture = YES; // 大图
            }
            
            // 计算图片控件的frame
            CGFloat pictureX = OSTopicCellMargin;
            CGFloat pictureY = OSTopicCellTextY + textH + OSTopicCellMargin;
            _pictureF = CGRectMake(pictureX, pictureY, pictureW, pictureH);
            
            _cellHeight += pictureH + OSTopicCellMargin;
        } else if (self.type == OSTopicTypeVoice) { // 声音帖子
            CGFloat voiceX = OSTopicCellMargin;
            CGFloat voiceY = OSTopicCellTextY + textH + OSTopicCellMargin;
            CGFloat voiceW = maxSize.width;
            CGFloat voiceH = voiceW * self.height / self.width;
            _voiceF = CGRectMake(voiceX, voiceY, voiceW, voiceH);
            _cellHeight += voiceH + OSTopicCellMargin;
        }else if (self.type == OSTopicTypeVideo){
            
            CGFloat videoX = OSTopicCellMargin;
            CGFloat videoY = OSTopicCellTextY + textH + OSTopicCellMargin;
            CGFloat videoW = maxSize.width;
            CGFloat videoH = videoW * self.height / self.width;
            _videoF = CGRectMake(videoX, videoY, videoW, videoH);
            _cellHeight += videoH + OSTopicCellMargin;
        }
        
        // 底部工具条的高度
        _cellHeight += OSTopicCellBottomBarH + OSTopicCellMargin;
    }
    return _cellHeight;
}
@end
